package com.yidu.hbats.service.TrainingPlan;/**
 * @类的描述——
 * @author 朱星润
 * @versing 1.0 2019/10/30
 */

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.action.vo.AtsPlanWeekvo;
import com.yidu.hbats.dao.AtsPlanWeekMapper;
import com.yidu.hbats.model.AtsEmpAthleteScheme;
import com.yidu.hbats.model.AtsPlanCurriculum;
import com.yidu.hbats.model.AtsPlanItem;
import com.yidu.hbats.model.AtsPlanWeek;
import com.yidu.springbootdemo.permission.dao.TuserMapper;
import com.yidu.springbootdemo.permission.domain.Tuser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 朱星润
 * @类的描述——
 * @date 2019/10/30下午 04:02
 */
@Service
public class AtsPlanWeekServiceImpl implements AtsPlanWeekService {
    @Resource
    AtsPlanWeekMapper atsPlanWeekMapper;
    //训练指标
    @Resource
    AtsPlanItemService atsPlanItemService;
    @Resource
    TuserMapper managerModelMapper;
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    @Override
    public int count(HttpServletRequest request, AtsPlanWeek atsPlanWeek) {

        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        int count = 0;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
            if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
                atsPlanWeek.setCoachName(manager.getCompanyName());
                atsPlanWeek.setCoachId(Integer.valueOf(manager.getFilId()));
                count = atsPlanWeekMapper.countCoach(atsPlanWeek);
            } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
                atsPlanWeek.setAthleteName(manager.getCompanyName());
                atsPlanWeek.setAthleteId(Integer.valueOf(manager.getFilId()));
                count = atsPlanWeekMapper.countAthlete(atsPlanWeek);
            } else {
                count = atsPlanWeekMapper.countAll(atsPlanWeek);
            }
        } else {
            count = atsPlanWeekMapper.countAll(atsPlanWeek);
        }
        return count;
    }
    @Override
    public List<AtsPlanWeek> selectAll(HttpServletRequest request,AtsPlanWeek atsPlanWeek,Integer pageNum, Integer pageSize) {

        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        PageHelper.startPage(pageNum, pageSize);
        List<AtsPlanWeek> list = null;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
            if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
                atsPlanWeek.setCoachName(manager.getCompanyName());
                atsPlanWeek.setCoachId(Integer.valueOf(manager.getFilId()));
                list = atsPlanWeekMapper.findCoach(atsPlanWeek);
            } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
                atsPlanWeek.setAthleteName(manager.getCompanyName());
                atsPlanWeek.setAthleteId(Integer.valueOf(manager.getFilId()));
                list = atsPlanWeekMapper.findAthlete(atsPlanWeek);
            } else {
                list = atsPlanWeekMapper.selectAll(atsPlanWeek);
            }
        } else {
            list = atsPlanWeekMapper.selectAll(atsPlanWeek);
        }


        return list;
    }
    @Override
    public List<AtsPlanWeek> findIdTwo(String creater) {
        return atsPlanWeekMapper.findIdTwo(creater);
    }


    @Override
    public int updateIsva(AtsPlanWeek atsPlanWeek) {
        atsPlanWeek.setIsva(0);
        int rows = atsPlanWeekMapper.updateIsva(atsPlanWeek);

        return rows;
    }
    @Override
    public int delAll(String id) {
        String[] ids = id.split(",");
        int rows=0;

        for(int i = 0;i<ids.length;i++){
            //删除计划同时删除下面指标
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setIsva(0);
            atsPlanItem.setPid(Integer.valueOf(ids[i]));
            //删除对应的运动员
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(Integer.valueOf(ids[i]));

            AtsPlanWeek atsPlanWeek=new AtsPlanWeek();
            atsPlanWeek.setWeekId(Integer.valueOf(ids[i]));
            atsPlanWeek.setIsva(0);
            rows=updateIsva(atsPlanWeek);
            atsEmpAthleteSchemeService.updatePidFour(atsEmpAthleteScheme);
            atsPlanItemService.updateOrdeletethree(atsPlanItem);
            rows++;
        }
        return rows;
    }

    @Override
    public void insertSelective(AtsPlanWeekvo record) {
        AtsPlanWeek atsPlanWeek=new AtsPlanWeek();
        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        record.setCreatTime(sqlDate);
        BeanUtils.copyProperties(record,atsPlanWeek);
        atsPlanWeekMapper.insertSelective(atsPlanWeek);
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setRemark("2");
        atsPlanItem.setIsva(1);
        atsPlanItem.setCreatTime(sqlDate);
        atsPlanItem.setCreater(record.getCreater());
        //周计划计划传3 靠这个区别和查询
        atsPlanItem.setPlandate("3");
        atsPlanItem.setPid(atsPlanWeek.getWeekId());
        atsPlanItem.setPname(atsPlanWeek.getWeekName());
        boolean f=record.getItemNameone().contains(",");
        if(f==true){
            String[] arr= record.getItemNameone().split(",");
            String[] arrtwo= record.getItemValueone().split(",");
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i];
                atsPlanItem.setItemName(s);
                String sa = arrtwo[i];
                atsPlanItem.setItemValue(sa);
                atsPlanItemService.insertSelective(atsPlanItem);
            }

        }else{
            atsPlanItem.setItemName(record.getItemNameone());
            atsPlanItem.setItemValue(record.getItemValueone());
            atsPlanItemService.insertSelective(atsPlanItem);
        }
        //new出计划教练员关联表
        AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
        atsEmpAthlete.setCreater(record.getCreater());
        //传入2代表周训练计划
        atsEmpAthlete.setRemark("2");
        atsEmpAthlete.setPname(record.getWeekName());
        atsEmpAthlete.setPid(atsPlanWeek.getWeekId());
        boolean flag=record.getAthleteId().contains(",");
        if(flag==true) {
            String[] athleteId = record.getAthleteId().split(",");
            String[] athleteName = record.getAthleteName().split(",");
            for (int i = 0; i < athleteId.length; i++) {
                String athleteIdone = athleteId[i];
                String athleteNameone = athleteName[i];
                atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                atsEmpAthlete.setAthleteName(athleteNameone);
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }else{
            atsEmpAthlete.setAthleteId(Integer.valueOf(record.getAthleteId()));
            atsEmpAthlete.setAthleteName(record.getAthleteName());
            atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
        }
    }

    @Override
    public void update(AtsPlanWeekvo atsPlanWeekvo) {
        AtsPlanWeek atsPlanWeek=new AtsPlanWeek();
        Date utilDate = new Date();//util.Date
        atsPlanWeek.setSportId(atsPlanWeekvo.getSportId());
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        atsPlanWeekvo.setCreatTime(sqlDate);
        BeanUtils.copyProperties(atsPlanWeekvo,atsPlanWeek);
        atsPlanWeekMapper.updateIsva(atsPlanWeek);
        AtsPlanItem atsPlanItemt=new AtsPlanItem();
        atsPlanItemt.setIsva(0);
        atsPlanItemt.setPid(atsPlanWeekvo.getWeekId());
        int rows=atsPlanItemService.updateOrdeletethree(atsPlanItemt);
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(atsPlanWeekvo.getWeekId());
        int rowsone=atsEmpAthleteSchemeService.updatePidFive(atsEmpAthleteScheme);
        if(rowsone>0){
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete=new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(atsPlanWeekvo.getCreater());
            //传入2代表周训练计划
            atsEmpAthlete.setRemark("2");
            atsEmpAthlete.setPname(atsPlanWeekvo.getWeekName());
            atsEmpAthlete.setPid(atsPlanWeekvo.getWeekId());
            boolean flag=atsPlanWeekvo.getAthleteId().contains(",");
            if(flag==true) {
                String[] athleteId = atsPlanWeekvo.getAthleteId().split(",");
                String[] athleteName = atsPlanWeekvo.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            }else{
                atsEmpAthlete.setAthleteId(Integer.valueOf(atsPlanWeekvo.getAthleteId()));
                atsEmpAthlete.setAthleteName(atsPlanWeekvo.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }
        if(rows>0){
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setRemark("2");
            atsPlanItem.setIsva(1);
            atsPlanItem.setCreatTime(sqlDate);
            atsPlanItem.setCreater(atsPlanWeekvo.getCreater());
            //周计划传3靠这个区别和查询
            atsPlanItem.setPlandate("3");
            atsPlanItem.setPid(atsPlanWeek.getWeekId());
            atsPlanItem.setPname(atsPlanWeek.getWeekName());
            boolean f=atsPlanWeekvo.getItemNameone().contains(",");
            if(f==true){
                String[] arr= atsPlanWeekvo.getItemNameone().split(",");
                String[] arrtwo= atsPlanWeekvo.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    atsPlanItem.setItemName(s);
                    String sa = arrtwo[i];
                    atsPlanItem.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItem);
                }

            }else{
                atsPlanItem.setItemName(atsPlanWeekvo.getItemNameone());
                atsPlanItem.setItemValue(atsPlanWeekvo.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItem);
            }
        }else{
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setRemark("2");
            atsPlanItem.setIsva(1);
            atsPlanItem.setCreatTime(sqlDate);
            atsPlanItem.setCreater(atsPlanWeekvo.getCreater());
            //周计划传3 靠这个区别和查询
            atsPlanItem.setPlandate("3");
            atsPlanItem.setPid(atsPlanWeek.getWeekId());
            atsPlanItem.setPname(atsPlanWeek.getWeekName());
            boolean f=atsPlanWeekvo.getItemNameone().contains(",");
            if(f==true){
                String[] arr= atsPlanWeekvo.getItemNameone().split(",");
                String[] arrtwo= atsPlanWeekvo.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    atsPlanItem.setItemName(s);
                    String sa = arrtwo[i];
                    atsPlanItem.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItem);
                }

            }else{
                atsPlanItem.setItemName(atsPlanWeekvo.getItemNameone());
                atsPlanItem.setItemValue(atsPlanWeekvo.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItem);
            }
        }
    }

    @Override
    public AtsPlanWeek selectByPrimaryKey(Integer weekId) {
        return atsPlanWeekMapper.selectByPrimaryKey(weekId);
    }

    @Override
    public List<AtsPlanWeek> selectAll(AtsPlanWeek atsPlanWeek) {
        return atsPlanWeekMapper.selectAll(atsPlanWeek);
    }

    @Override
    public AtsPlanWeek findMaxPid(int coachId) {
        return atsPlanWeekMapper.findMaxPid(coachId);
    }
}
